//
//  RateListVC.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/10/12.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

class RateListVC: BaseVC {
    
    @IBOutlet weak var tableView: UITableView!
    
    var selectRate:Rate?
    var selectFinished:(()->Void)?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        setupData()
    }
    
    private func setupUI() {
        
        tableView.backgroundColor = kThemeBgColor
        
        tableView.register(UINib(nibName: "RateListCell", bundle: nil), forCellReuseIdentifier: "RateListCell")
        
        tableView.tableFooterView = UIView()
        
        tableView.rowHeight = 64
    }
    
    private func setupData() {
        
        self.title = LocalizedString("switch_currency")
        
        RateManager.share.dealRateList()
        
        RateManager.share.dealRateListFinished = {
            [weak self] in
            guard let `self` = self else { return }
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5, execute: {
                self.tableView.reloadData()
            })
            
        }
    }
}

extension RateListVC: UITableViewDataSource, UITableViewDelegate {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return RateManager.share.rateFistLetterArray.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        let key = RateManager.share.rateFistLetterArray[section]
        let array = RateManager.share.rateDict[key]
        
        return array?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "RateListCell") as! RateListCell
        
        let key = RateManager.share.rateFistLetterArray[indexPath.section]
        let array = RateManager.share.rateDict[key]
        
        cell.rate = array?[indexPath.row]
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        
        let array = RateManager.share.rateFistLetterArray
        let key = array[section]
        
        if key == "#" {
            
            return LocalizedString("hot_currency")
        }
        
        return key
    }
    
    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
    {
        let header:UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
        
        header.textLabel?.textColor = UIColor.hexStringToColor(hexString: "#222222").withAlphaComponent(0.5
        )
        header.textLabel?.font = kFont12
    }
    
    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        
        return RateManager.share.rateFistLetterArray
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let key = RateManager.share.rateFistLetterArray[indexPath.section]
        let array = RateManager.share.rateDict[key]
        
        let storeRateList = RateManager.share.storeRateList
        
        guard let rate = array?[indexPath.row], let old = selectRate else { return }
        
        if storeRateList?.contains(rate) ?? false {
            
            self.view.makeToast(LocalizedString("add_currency_error_tips"))
            return
        }
        
        RateManager.share.storRate(new: rate, old: old)
        
        self.selectFinished?()
        
        self.navigationController?.popViewController(animated: true)
    }
}
